package LobbyNode

import (
	"PBMessage"
	"Player"

	"git.oschina.net/yangdao/goerl"

	"git.oschina.net/yangdao/SGNet2"
	"git.oschina.net/yangdao/extlib/log"
	"github.com/golang/protobuf/proto"
)

func (lobbyNode *LobbyNode) loginHandler(msg proto.Message, sess *SGNet2.Session) {
	loginData := msg.(*PBMessage.CS_Login)
	if lobbyNode.getPlayer(loginData.GetDeviceId()) != nil {
		log.Log("LobbyNode,Info", "玩家重复登录"+loginData.GetDeviceId())
		return
	}

	goerl.SendByName("DBNode", []interface{}{":req-register-login", loginData.GetDeviceId(), sess.ID})
}

func (lobbyNode *LobbyNode) dbLoginHandler(tuple []interface{}) {
	//deviceID := tuple[2].(string)
	nickName := tuple[3].(string)
	//level := tuple[4].(int)
	sessID := tuple[5].(int64)
	scLogin := &PBMessage.SC_Login{}
	scLogin.NickName = proto.String(nickName)
	scLogin.UserID = proto.Int64(sessID)
	scLogin.Type = proto.Int32(0)
	scLogin.Rank = proto.Int32(1000)
	player := &Player.GamePlayer{
		GUID:      sessID,
		SessID:    sessID,
		NickName:  nickName,
		DevciveID: tuple[6].(string),
	}
	lobbyNode.playerList[player.GUID] = player
	player.State = Player.PlayerStateLobby
	goerl.SendByName("NetNode", []interface{}{":ToClientMail", sessID, int(PBMessage.PBID_SC_Login), scLogin})
	goerl.SendByName("NetNode", []interface{}{":CmdMail", ":player-state-change", player.SessID, player.State})
	lobbyNode.sendTick()
	log.Log("LobbyNode,Info", "登录成功"+player.DevciveID)
}
